﻿using ProtoBuf.Message;
using System.Collections;
using System.Collections.Generic;
using Table.Blo;
using UnityEngine;
namespace AutoChess
{
    public static class UtilityHelper
    {
        public static void TryGetHerooffset(int heroId,GridType gridType, out Vector3 localPos,out Vector3 localscale,out Vector3 localAngle)
        {
            if (gridType == GridType.ShopGrid)
            {
                AutoChessBlo.GetHeroOffset(heroId, out localPos, out localscale, out localAngle);
            }
            else
            {
                localscale = Vector3.one;
                var dictModel = Table.Blo.TableSailorModelBlo.GetTableSailorModelByHeroIdAndAppearence(heroId, 0);
                if (dictModel != null)
                {
                    //设置模型偏移等默认值
                    if (dictModel != null)
                    {
                        localscale *= 0.85f * (float)dictModel.ModelScale * AutoChessBlo.GetFieldScale(heroId);
                    }
                }
                var angleY =gridType == GridType.PreChooseGrid ? 90 : 0;
                if (gridType == GridType.PreChooseGrid)
                {
                    angleY = 180;
                }
                else if (gridType == GridType.FightGrid)
                {
                    angleY = 90;
                }
                else if (gridType == GridType.EnemyGrid)
                {
                    angleY = -90;
                }
                localAngle = new Vector3(0,angleY,0);
                localPos = Vector3.zero;
            }
        }
        
        public static void Normalize(this Transform transform)
        {
            transform.localPosition = Vector3.zero;
            transform.localScale = Vector3.one;
            transform.localEulerAngles = Vector3.zero;
        }
        public static void SetUnused(this Transform transform,bool active,string originName)
        {
            transform.Normalize();
            transform.name = active ? originName:string.Format( "{0},{1}",GameConst.AC_UNUSED,GameConst.AC_UNUSED );
            transform.gameObject.SetActive(active);
        }

        public static GridType GetGridType(AutoChessAreaType type)
        {
            switch (type)
            {
                case AutoChessAreaType.AUTO_CHESS_AREA_WAITING:
                    return GridType.PreChooseGrid;
                case AutoChessAreaType.AUTO_CHESS_AREA_FIGHT:
                    return GridType.FightGrid;
                case AutoChessAreaType.AUTO_CHESS_AREA_BUG:
                    return GridType.ShopGrid;
                default:
                    {
                        Debug.LogError(type + " 类型错误");
                    }
                    return GridType.ShopGrid;

            }
        }

        /// <summary>
        /// 和服务器约定的规则，取这个格子的区域
        /// </summary>
        /// <param name="serverPos"></param>
        /// <returns></returns>
        public static AutoChessAreaType GetAreaTypeFromPos(int serverPos)
        {
            return (AutoChessAreaType)((int)(serverPos / 100)); ;
        }
        public static GridType GetGridTypeFromPos(int serverPos)
        {
            return (GridType)((int)(serverPos / 100)); ;
        }
        /// <summary>
        /// 把服务器格子位置转换成客户端位置. 服务器从1开始
        /// </summary>
        /// <param name="serverPos"></param>
        /// <returns></returns>
        public static int GetClientPosFromServer(int serverPos)
        {
            return serverPos % 100;
        }

        public static int ConvertServerPosFromClientPos(int clientPos, GridType gridType)
        {
            return clientPos;
        }
        public static CommonEffectResId GetEffectIdByHeroRare(int heroRare)
        {
            if (heroRare == 1)
                return CommonEffectResId.AC_Hero_Bai;
            else if (heroRare == 2)
                return CommonEffectResId.AC_Hero_Lv;
            else if (heroRare == 3)
                return CommonEffectResId.AC_Hero_Lan;
            else if (heroRare == 4)
                return CommonEffectResId.AC_Hero_Zi;
            else if (heroRare == 5)
                return CommonEffectResId.AC_Hero_Cheng;

            return   CommonEffectResId.AC_Hero_Bai;
        }
        public static CommonEffectResId GetEffectIdByHeroRare(HeroEffectType type)
        {
            if (type == HeroEffectType.Field2Hero)
                return CommonEffectResId.AC_Hero_Have;
            else if (type == HeroEffectType.Field3Chess)
                return CommonEffectResId.AC_Hero_UI_Upgrade;
            return CommonEffectResId.None;
        }
        public static CommonEffect PlayEffect(CommonEffectResId resId, Transform root)
        {
            return CommonEffectManager.Instance.PlayEffectImmediate(resId, root);
        }
        public static void PlayUIAudio(string clipName)
        {
            Yunchang.UIAudioManager.Instance.PlayUIAudio(clipName, 1);
        }
        private const int defaultShipId = 130006;
        public static string GetShipIcon(ACPlayerData data)
        {
            int shipId = defaultShipId;
            if (data.ShipId != 0)
            {
                shipId = data.ShipId;
            }

            var shipTable = Table.Blo.TableShipBlo.GetTableShip(shipId);
            if (shipTable != null)
            {
                return shipTable.shipIcon;
            }
            return string.Empty;
        }
        public static string GetShipIcon(int shipId)
        {
            if (shipId == 0)
            {
                shipId = defaultShipId;
            }

            var shipTable = Table.Blo.TableShipBlo.GetTableShip(shipId);
            if (shipTable != null)
            {
                return shipTable.shipIcon;
            }
            return string.Empty;
        }
        public static void PlayBackGround()
        {
            AudioTool.Instance.LoadAndPlayBackgroundMusic(AutoChessFieldViewCtr.Ins.SceneConfig.backAudio);
        }

        /// <summary>
        /// 获取船的缩放
        /// </summary>
        /// <param name="shipId"></param>
        /// <returns></returns>
        public static float GetShipScale(int shipModelId)
        {
            var shipTable = Table.Blo.TableShipBlo.GetTableShipModel(shipModelId);
            return shipTable.AcShipScale;
        }

        public static Vector3 GetShipModelOffset(int shipModelId,bool left)
        {
            var shipTable = Table.Blo.TableShipBlo.GetTableShipModel(shipModelId);
            return CommonMethod.GetPosition(left ? shipTable.ACLeftPosOffset: shipTable.AcRightPosOffset );
        }

        public static Vector3 GetShipModelHitPos(int shipModelId)
        {
            var shipTable = Table.Blo.TableShipBlo.GetTableShipModel(shipModelId);
            return CommonMethod.GetPosition( shipTable.hitPosOffset);
        }

        public static Vector3 GetShipModelFireOffset(int shipModelId, bool left)
        {
            var shipTable = Table.Blo.TableShipBlo.GetTableShipModel(shipModelId);
            return CommonMethod.GetPosition(left ? shipTable.leftFirePosOffset: shipTable.rightFirePosOffset);
        }

        public static CommonAnimCurve GetComeCurve()
        {
            var leftPos = AutoChessFieldViewCtr.Ins.SceneConfig.targetPos;
            var comePosStart = new Vector3(leftPos.position.x, leftPos.position.y, leftPos.position.z + 50);
            var cuvre = AutoChessFieldViewCtr.Ins.SceneConfig.shipCome;
            cuvre.startPos = comePosStart;
            cuvre.endPos = leftPos.position;
            return cuvre;
        }

        public static CommonAnimCurve GetLeaveCurve()
        {
            var leftPos = AutoChessFieldViewCtr.Ins.SceneConfig.targetPos;
            var leavePosStart = new Vector3(leftPos.position.x, leftPos.position.y, leftPos.position.z - 50);
            var cuvre = AutoChessFieldViewCtr.Ins.SceneConfig.shipCome;
            cuvre.startPos = leftPos.position ;
            cuvre.endPos = leavePosStart ;
            return cuvre;
        }

    }
}
